import { action, makeAutoObservable } from 'mobx';
import { get, isEmpty } from 'lodash';

import request from '@utils/request';
import List from './base.list';
import { AxiosRequestConfig } from 'axios';

interface IfetchList {
  page?: number;
  limit?: number;
  order?: string; // 排序
  namespace: string; // 请求地址
  more?: true; // more: 在原有数据添加，如下拉加载
}

interface INamespace {
  namespace: string;
}
export default class BaseStore {
  list = new List();

  isSubmitting = true;
  // isLoading = true

  constructor() {
    makeAutoObservable(this);
  }

  // 接口前缀
  get apiVersion() {
    return 'apis/apps/v1';
  }

  getListUrl = ({ namespace }: { namespace: string }) =>
    `${this.apiVersion}/${namespace}`;
  getSubUrl = ({ namespace }: { namespace: string }) =>
    `${this.apiVersion}/${namespace}`;

  submitting = (promise: Promise<any>) => {
    this.isSubmitting = true;

    setTimeout(() => {
      promise
        // eslint-disable-next-line @typescript-eslint/no-empty-function
        .catch(() => {})
        .finally(() => {
          this.isSubmitting = false;
        });
    }, 500);

    return promise;
  };
  async fetchListPage({
    limit,
    page,
    order,
    namespace,
    more,
    ...filters
  }: IfetchList) {
    this.list.isLoading = true;

    const params: any = {};

    if (!isEmpty(filters)) {
      params.filters = filters;
    }
    if (limit !== Infinity) {
      params.paging = `limit=${limit || 10},page=${page || 1}`;
    }

    if (order) {
      params.orderBy = order;
    }

    const result: { total_count: number; items: any[] } = await request.get(
      this.getListUrl({ namespace }),
      params
    );
    const data = get(result, 'items', []);
    this.list.update({
      data: more ? [...this.list.data, ...data] : data,
      total: result.total_count || 0,
      limit: Number(limit) || 10,
      page: Number(page) || 1,
      order,
      filters,
      isLoading: false,
    });
  }
  async fetchList({
    namespace,
    labelSelector,
    ...rest
  }: {
    labelSelector: any;
    namespace: string;
  }) {
    this.list.isLoading = true;
    if (!namespace) {
      this.list.isLoading = false;
      return;
    }

    const params: any = rest;

    if (!isEmpty(labelSelector)) {
      params.labelSelector = labelSelector;
    }

    const result: { total_count: number; items: any[] } = await request.get(
      this.getListUrl({ namespace }),
      params
    );
    const data = get(result, 'items', []);
    this.list.update({
      data,
      total: result.items.length,
      isLoading: false,
    });
  }

  create(data: any) {
    const namespace = get(data, 'namespace');
    if (!namespace) {
      return;
    }
    return this.submitting(request.post(this.getSubUrl({ namespace }), data));
  }
  update({ namespace }: INamespace, newObject: any) {
    return this.submitting(
      request.put(this.getSubUrl({ namespace }), newObject)
    );
  }

  delete({ namespace }: INamespace) {
    return this.submitting(request.delete(this.getSubUrl({ namespace })));
  }
}
